home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / openoffice / program / pythonloader.py < prev    next >
Encoding:
Python Source  |  2007-04-10  |  5.1 KB  |  134 lines

  1. #*************************************************************************
  2. #
  3. #   OpenOffice.org - a multi-platform office productivity suite
  4. #
  5. #   $RCSfile: pythonloader.py,v $
  6. #
  7. #   $Revision: 1.4 $
  8. #
  9. #   last change: $Author: rt $ $Date: 2005/09/08 16:50:23 $
  10. #
  11. #   The Contents of this file are made available subject to
  12. #   the terms of GNU Lesser General Public License Version 2.1.
  13. #
  14. #
  15. #     GNU Lesser General Public License Version 2.1
  16. #     =============================================
  17. #     Copyright 2005 by Sun Microsystems, Inc.
  18. #     901 San Antonio Road, Palo Alto, CA 94303, USA
  19. #
  20. #     This library is free software; you can redistribute it and/or
  21. #     modify it under the terms of the GNU Lesser General Public
  22. #     License version 2.1, as published by the Free Software Foundation.
  23. #
  24. #     This library is distributed in the hope that it will be useful,
  25. #     but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. #     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  27. #     Lesser General Public License for more details.
  28. #
  29. #     You should have received a copy of the GNU Lesser General Public
  30. #     License along with this library; if not, write to the Free Software
  31. #     Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  32. #     MA  02111-1307  USA
  33. #
  34. #*************************************************************************
  35. import uno
  36. import unohelper
  37. import imp
  38. import os
  39. from com.sun.star.uno import Exception,RuntimeException
  40. from com.sun.star.loader import XImplementationLoader
  41. from com.sun.star.lang import XServiceInfo
  42.  
  43. MODULE_PROTOCOL = "vnd.openoffice.pymodule:"
  44. DEBUG = 0
  45.  
  46. g_supportedServices  = "com.sun.star.loader.Python",      # referenced by the native C++ loader !
  47. g_implementationName = "org.openoffice.comp.pyuno.Loader" # referenced by the native C++ loader !
  48.  
  49. def splitUrl( url ):
  50.       nColon = url.find( ":" )
  51.       if -1 == nColon:
  52.             raise RuntimeException( "PythonLoader: No protocol in url " + url )
  53.       return url[0:nColon], url[nColon+1:len(url)]
  54.  
  55. g_loadedComponents = {}
  56.  
  57. class Loader( XImplementationLoader, XServiceInfo, unohelper.Base ):
  58.       def __init__(self, ctx ):
  59.       if DEBUG:
  60.          print "pythonloader.Loader ctor" 
  61.       self.ctx = ctx
  62.  
  63.       def getModuleFromUrl( self, url ):
  64.           if DEBUG:
  65.                 print "pythonloader: interpreting url " +url
  66.           protocol, dependent = splitUrl( url )
  67.           if "vnd.sun.star.expand" == protocol:
  68.                 exp = self.ctx.getValueByName( "/singletons/com.sun.star.util.theMacroExpander" )
  69.                 url = exp.expandMacros(dependent)
  70.                 protocol,dependent = splitUrl( url )
  71.  
  72.           if DEBUG:
  73.                 print "pythonloader: after expansion " +protocol +":" + dependent
  74.                 
  75.           try:
  76.                 if "file" == protocol:
  77.                       # remove \..\ sequence, which may be useful e.g. in the build env
  78.                       url = unohelper.absolutize( url, url )
  79.  
  80.                       # did we load the module already ?
  81.                       mod = g_loadedComponents.get( url )
  82.                       if not mod:
  83.                             mod = imp.new_module("uno_component")
  84.  
  85.                             # read the file
  86.                             fileHandle = file( unohelper.fileUrlToSystemPath( url ) )
  87.                             src = fileHandle.read()
  88.  
  89.                             # execute the module
  90.                             exec src in mod.__dict__
  91.                             g_loadedComponents[url] = mod
  92.                       return mod
  93.                 elif "vnd.openoffice.pymodule" == protocol:
  94.                       return  __import__( dependent )
  95.                 else:
  96.                       raise RuntimeException( "PythonLoader: Unknown protocol " +
  97.                                               protocol + " in url " +url, self )
  98.           except ImportError, e:
  99.                 raise RuntimeException( "Couldn't load "+url+ " for reason "+str(e), None)
  100.           return None
  101.        
  102.       def activate( self, implementationName, dummy, locationUrl, regKey ):
  103.       if DEBUG:
  104.          print "pythonloader.Loader.activate"
  105.  
  106.       mod = self.getModuleFromUrl( locationUrl )
  107.           implHelper = mod.__dict__.get( "g_ImplementationHelper" , None )
  108.           if implHelper == None:
  109.         return mod.getComponentFactory( implementationName, self.ctx.ServiceManager, regKey )
  110.           else:
  111.         return implHelper.getComponentFactory( implementationName,regKey,self.ctx.ServiceManager)
  112.          
  113.       def writeRegistryInfo( self, regKey, dummy, locationUrl ):
  114.       if DEBUG:
  115.          print "pythonloader.Loader.writeRegistryInfo"
  116.              
  117.       mod = self.getModuleFromUrl( locationUrl )
  118.           implHelper = mod.__dict__.get( "g_ImplementationHelper" , None )
  119.           if implHelper == None:
  120.             return mod.writeRegistryInfo( self.ctx.ServiceManager, regKey )
  121.           else:
  122.             return implHelper.writeRegistryInfo( regKey, self.ctx.ServiceManager )
  123.  
  124.       def getImplementationName( self ):
  125.       return g_implementationName
  126.  
  127.       def supportsService( self, ServiceName ):
  128.       return ServiceName in self.serviceNames
  129.  
  130.       def getSupportedServiceNames( self ):
  131.       return g_supportedServices
  132.  
  133.  
  134.